/*
** token_type.c for 42sh in /home/guina_b/
** 
** Made by benoit guina
** Login   <guina_b@epitech.net>
** 
** Started on  Fri Jan 01 00:00:00 2010 benoit guina
** Last update Fri Jan 01 00:00:00 2010 benoit guina
*/

#include <string.h>
#include "escaping.h"
#include "token_type.h"

t_token_type *token_types_get()
{
  static int i = -1;
  static t_token_type types[TOKEN_TYPES_TOTAL];

  if (i > -1)
    return (types);
  types[TOKEN_TILDE].sym = "~";
  types[TOKEN_ASTERISK].sym = "*";
  types[TOKEN_QUESTION].sym = "?";
  types[TOKEN_EXCLAMATION].sym = "!";
  types[TOKEN_DOLLAR].sym = "$";
  types[TOKEN_PAREN_L].sym = "(";
  types[TOKEN_PAREN_R].sym = ")";
  types[TOKEN_DOUBLE_AND].sym = "&&";
  types[TOKEN_DOUBLE_OR].sym = "||";
  types[TOKEN_DOUBLE_LESSER].sym = "<<";
  types[TOKEN_DOUBLE_GREATER].sym = ">>";
  types[TOKEN_AND].sym = "&";
  types[TOKEN_OR].sym = "|";
  types[TOKEN_LESSER].sym = "<";
  types[TOKEN_GREATER].sym = ">";
  types[TOKEN_TEXT].sym = "";
  while (++i < TOKEN_TYPES_TOTAL)
    types[i].len = strlen(types[i].sym);
  return (types);
}

t_token_type *token_type_identify(char *s, int pos)
{
  int i;
  t_token_type *types;

  types = token_types_get();
  i = -1;
  while (*types[++i].sym &&
      !(is_escaped(s, pos) == ESCAPING_NONE &&
        !strncmp(s + pos, types[i].sym, types[i].len)));
  return (types + i);
}
